package com.shardingsphere.shardingsphere.myShardingAlgorithm;

import io.shardingsphere.api.algorithm.sharding.PreciseShardingValue;
import io.shardingsphere.api.algorithm.sharding.standard.PreciseShardingAlgorithm;

import java.util.Collection;

public class DBAlgorithm  implements PreciseShardingAlgorithm<String> {
	@Override
	public String doSharding(Collection<String> collection, PreciseShardingValue<String> preciseShardingValue) {



		for (String tableName : collection) {

			if (tableName.endsWith(hash(preciseShardingValue.getValue()) % 2 + "")) {
				System.out.println("访问数据库"+tableName);
				return tableName;
			}
		}
		throw new IllegalArgumentException();


	}

	static final  int hash(Object key) {
		int h;
		return (key == null) ? 0 : (h = key.hashCode()) ^ (h >>> 16);
	}

	public static void main(String[] args) {
		System.out.println(hash(1)%2);
	}

}
